﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using Microsoft.SharePoint;
using System.Web;
using System.IO;
using System.Web.UI.HtmlControls;

namespace TRIG_MOD_CORRES.wsp.Corres_Register
{
    public partial class Corres_RegisterUserControl : UserControl
    {

        DataForCorresRegister objCls = new DataForCorresRegister();

        private string Type { get; set; }

        public string Entity { get; set; }
        public string CID { get; set; }  // Incoming Documents
        public string CTH { get; set; }  // Transfer History

        bool export = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            frmDt.MaxDate = DateTime.Today;
            toDt.MaxDate = DateTime.Today;

            Type = Request.QueryString["Source"];
            objCls.errorMsg = errorMsg;

            if (!IsPostBack)
            {
                ddlCompany.Items.Clear();
                if (Type == "CNT") { ddlCompany.Items.Add("CNS"); ddlCompany.Items.Add("CLN"); }
                else if (Type == "CNS") { ddlCompany.Items.Add("CNT"); ddlCompany.Items.Add("CLN"); }
                else { ddlCompany.Items.Add("CNT"); ddlCompany.Items.Add("CNS"); }
            }
            SetListValues();
        }

        protected void rdCompany_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        public void SetListValues()
        {
            objCls.Sent_Receive = sent_receive;
            objCls.frmDate = ToSPDate(Convert.ToString(frmDt.SelectedDate));
            objCls.toDate = ToSPDate(Convert.ToString(toDt.SelectedDate));
            objCls.company = ddlCompany.SelectedItem.Text;

            if (Type == "CNT")
            {
                Entity = "Contractor";
                CID = "Contractor Incoming Documents";
                CTH = "Contractor Transfer History";
            }
            else if (Type == "CNS")
            {
                Entity = "Consultant";
                CID = "Consultant Incoming Documents";
                CTH = "Consultant Transfer History";
            }
            else
            {
                Entity = "Client";
                CID = "Client Incoming Documents";
                CTH = "Contractor Transfer History";
            }
        }

        protected void btnSummary_Click(object sender, EventArgs e)
        {
            DisableControls();
            objCls.subject = txtSubject.Text;
            if (frmDt.SelectedDate > toDt.SelectedDate)
            {
                errorMsg.Text = "From date should be less than to date"; errorMsg.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                try
                {
                    if (sent_receive.SelectedItem.Value == "0")

                        objCls.BindDate(gvCorres, CID);
                    else
                        objCls.BindDate(gvCorres, CTH);
                }
                catch (Exception x)
                {
                    errorMsg.Text = x.Message;
                }
            }

            if (gvCorres.Rows.Count > 0) btnExport.Visible = true; else btnExport.Visible = false;
        }

        private static String ToSPDate(String strDt)
        {
            if (strDt == String.Empty)
                return strDt;
            else
                return (Convert.ToDateTime(strDt)).ToString("yyyy-MM-dd");
        }

        protected void ddlCompany_SelectedIndexChanged(object sender, EventArgs e)
        {
            DisableControls();
        }

        protected void sent_receive_SelectedIndexChanged(object sender, EventArgs e)
        {
            DisableControls();
        }

        private void DisableControls()
        {
            errorMsg.Text = "";
            gvCorres.DataSource = null;
            gvCorres.Visible = false;
        }

        protected void gvCorres_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                SPFieldUrlValue docURL = new SPFieldUrlValue(e.Row.Cells[6].Text);
                string listurl = "javascript:return DialogDisplay('" + docURL.Url + "');";
                objCls.CreateLinkButton("View Properties", listurl, e, 4, true);

            }
        }

        protected void gvCorres_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            gvCorres.PageIndex = e.NewSelectedIndex;
            gvCorres.DataBind();
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                //this.export = true;
                //this.OnLoad(e);
                ExportToExcel("Report.xls", gvCorres);

            }
            catch (Exception x)
            {
                errorMsg.Text = "Export" + x.Message;
            }
        }

        private void ExportToExcel(string strFileName, GridView dg)
        {

        }


        //protected override void Render(HtmlTextWriter writer)
        //{
        //    try
        //    {
        //        if (Page != null)
        //        {
        //            if (export)
        //            {
        //                Page.Response.Clear();
        //                Page.Response.Buffer = true;
        //                Page.Response.ContentType = "application/ms-excel";
        //                Page.Response.AddHeader("content-disposition", "attachment; filename=HR Report " + DateTime.Now.ToString("dd/MM/yyyy") + ".xls");
        //                Page.Response.Charset = "UTF-8";
        //                DateTime ct = DateTime.Now;
        //                string currentdate = Convert.ToString(ct);
        //                HttpContext.Current.Response.Write("<b><h1 style='Text-align:left; padding-left:65px; color: #008000; font-weight: bold; font-style: normal; font-size: large'>Correspondance Register</b></h1>");
        //                this.EnableViewState = false;
        //                System.IO.StringWriter sw = new System.IO.StringWriter();
        //                System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
        //                gvCorres.AllowPaging = false;
        //                gvCorres.RenderControl(htw);
        //                Page.Response.Write(sw.ToString() + "<br/><br/><b>Report Date : " + currentdate + "</b>");
        //                //Page.Response.End();
        //                gvCorres.AllowPaging = true;
        //            }
        //        }
        //        base.Render(writer);
        //    }

        //    catch (Exception x)
        //    {
        //        errorMsg.Text = "Export in Excel render function" + x.Message;
        //    }
        //}

        protected void btnExport_Click1(object sender, EventArgs e)
        {

        }
    }
}
